package com.stedu.mall.orderservice.service;

import com.stedu.mall.common.bean.Cart;
import com.stedu.mall.common.bean.Goods;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.CartService;
import com.stedu.mall.common.service.GoodsService;
import com.stedu.mall.orderservice.mapper.CartMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl implements CartService {
   @Autowired
   private CartMapper cartMapper;
   @DubboReference
   private GoodsService goodsService;

    @Override
    public boolean insertOrUpdate(Cart cart) {
       Cart c = cartMapper.selectByUserIdAndByUserId(cart.getUserId(), cart.getGoodsId());
       if(c == null){
           cart.setCount(1);
       return cartMapper.insert(cart)==1;
       }else {
           c.setCount(c.getCount()+1);
          return cartMapper.update(c)==1;
       }
    }

    @Override
    public boolean delete(Integer id, Integer userId) throws SteduException {
       //判断购物车是否存在
        Cart cart = cartMapper.selectById(id);
        if(cart == null){
            throw new SteduException("该购物车不存在，无法删除");
        }
        //判断被删除的购物车是否属于当前用户
        if(!cart.getUserId().equals(userId)){
            throw new SteduException("该购物车不属于该用户，无法删除");
        }


        return cartMapper.delete(id) == 1;
    }

    @Override
    public boolean deleteByIds(List<Integer> ids, Integer userId) throws SteduException {
        //判断购物车是否存在
        for(Integer id:ids){
            Cart cart = cartMapper.selectById(id);
            if(cart == null){
                throw new SteduException("该购物车不存在，无法删除");
            }
            //判断被删除的购物车是否属于当前用户
            if(!cart.getUserId().equals(userId)){
                throw new SteduException("该购物车不属于该用户，无法删除");
            }
        }
        return cartMapper.deleteByIds(ids)==ids.size();
    }

    @Override
    public boolean update(Cart cart,Integer userId ) throws SteduException {
        //判断该购物车是否存在
        Cart c=cartMapper.selectById(cart.getId());
        if (c == null){
            throw  new SteduException("该购物车记录不存在,无法修改");
        }
        if(!c.getUserId().equals(userId)){
            throw new SteduException("该购物车不属于该用户，无法修改");
        }

        Goods goods = goodsService.selectById(c.getGoodsId());
        if(cart.getCount() != null && cart.getCount()>goods.getCount()){
            //当购物车商品数量超过库存就设置为库存数量
            cart.setCount(goods.getCount());
        }
        return cartMapper.update(cart) == 1;
    }

    @Override
    public Cart selectById(Integer id) {
        return cartMapper.selectById(id);
    }

    @Override
    public List<Cart> search(Cart condition) {
    List<Cart> cartList= cartMapper.selectByCondition(condition);
    cartList.stream()
            .forEach(cart -> {
                Goods goods = goodsService.selectById(cart.getGoodsId());
              cart.setGoods(goods);
            });
   return cartList;
    }

    @Override
    public List<Cart> selectByIds(Integer[] cartIds, Integer userId) throws SteduException {
        List<Cart> cartList=cartMapper.selectByIds(cartIds);
        //判断当前购物车是否属于当前用户
        for(Cart cart:cartList){
            if(!cart.getUserId().equals(userId)){
                throw new SteduException("非法购物车--");
            }
            //查询并设置购物车信息
            Goods goods =goodsService.selectById(cart.getGoodsId());
            cart.setGoods(goods);
        }
        return cartList;
    }


}
